/*
 * 状态栏示例程序
 * 展示了简单状态栏和高级状态栏的实现与切换
 */
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import QtQuick.Layouts 1.0
import QtQuick.Window 2.15

// 应用程序主窗口
ApplicationWindow {
    id: window

    // 进度值属性
    property int progress: 0
    // 控制使用的状态栏类型
    property bool useAdvancedHeader: false

    width: 800
    height: 600
    visible: true
    title: qsTr("状态栏示例")

    // 简单状态栏组件定义
    Component {
        id: simpleHeader

        Label {
            id: stausBar

            text: "就绪"
            padding: 10

            // 设置背景色
            background: Rectangle {
                color: Material.backgroundColor
            }

        }

    }

    // 高级状态栏组件定义
    Component {
        id: advancedHeader

        ToolBar {
            // 状态栏署名别名，方便外部访问和修改
            property alias statusText: mainStatusLabel.text
            property alias statusColor: mainStatusLabel.color
            property alias progressVisible: progressLabel.visible
            property alias progressText: progressLabel.text
            property alias timeText: timeLabel.text
            property bool isPaused: false

            RowLayout {
                anchors.fill: parent
                anchors.leftMargin: 5
                anchors.rightMargin: 5
                spacing: 10

                Label {
                    id: mainStatusLabel

                    text: "应用程序准备就绪"
                    elide: Text.ElideRight
                    Layout.fillWidth: true
                }

                // 分割符
                Label {
                    text: "|"
                    color: "gray"
                }

                // 进度条
                Label {
                    id: progressLabel

                    text: "进度： 0%"
                    visible: false
                }


                // 时间
                Label {
                    id: timeLabel

                    text: "时间：" + Qt.formatDateTime(new Date(), " hh:mm:ss")
                }

            }

        }

    }

    // 主界面内容
    Column {

        anchors.centerIn: parent
        spacing: 10

        // 切换状态栏按钮
        Button {
            text: "切换状态栏"
            onClicked: {
                useAdvancedHeader = !useAdvancedHeader;
            }
        }

        // 显示消息按钮
        Button {
            text: "显示消息"
            onClicked: {
                if (useAdvancedHeader)
                    header.item.statusText = "操作已完成-" + new Date().toLocaleTimeString();
                else
                    header.item.text = "操作已完成-" + new Date().toLocaleTimeString();
            }
        }

        // 清空状态按钮
        Button {
            text: "清空状态"
            onClicked: {
                if (useAdvancedHeader)
                    header.item.statusText = "应用程序准备就绪";
                else
                    header.item.text = "就绪";
            }
        }

        Button {
            text: "显示错误"
            visible: useAdvancedHeader
            onClicked: {
                header.item.statusText = "错误：操作失败！";
                header.item.statusColor = "red";
                errorTimer.start();
            }
        }

        Button {
            text: "显示成功"
            visible: useAdvancedHeader
            onClicked: {
                header.item.statusText = "操作成功！";
                header.item.statusColor = "green";
                successTimer.start();
            }
        }

        Button {
            text: "开始任务"
            visible: useAdvancedHeader
            onClicked: {
                header.item.statusText = "任务进行中...";
                header.item.progressVisible = true;
                header.item.isPaused = false;
                progressTimer.start();
            }
        }

        Button {
            text: (header.item && header.item.isPaused !== undefined) ? "继续" : "暂停"
            visible: useAdvancedHeader && header.item && header.item.progressVisible
            onClicked: {
                if (header.item) {
                    header.item.isPaused = !header.item.isPaused;
                    header.item.statusText = header.item.isPaused ? "任务已暂停" : "任务进行中...";
                }
            }
        }

    }

    Timer {
        id: progressTimer

        interval: 100
        repeat: true
        onTriggered: {
            if (header.item && !header.item.isPaused) {
                progress += 1;
                if (header.item.progressText !== undefined)
                    header.item.progressText = "进度： " + progress + "%";

                if (progress >= 100) {
                    stop();
                    header.item.statusText = "任务已完成";
                    header.item.statusColor = "green";
                    progress = 0; // 修复：直接使用 progress
                    header.item.progressVisible = false;
                    header.item.isPaused = false;
                }
            }
        }
    }

    Timer {
        id: errorTimer

        interval: 3000
        repeat: false
        onTriggered: {
            header.item.statusText = "应用程序准备就绪";
            header.item.statusColor = "black";
        }
    }

    Timer {
        id: successTimer

        interval: 3000
        repeat: false
        onTriggered: {
            header.item.statusText = "应用程序准备就绪";
            header.item.statusColor = "black";
        }
    }

    Timer {
        interval: 1000
        repeat: true
        running: true
    }

    // 使用loader加载状态栏
    header: Loader {
        sourceComponent: useAdvancedHeader ? advancedHeader : simpleHeader
    }

}
